/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.variable.dtgenerator.vardtmGenerate.cmpgenerators;


import com.inspur.edp.bef.component.base.GspComponent;
import com.inspur.edp.bef.component.base.ReturnValue;
import com.inspur.edp.bef.component.detailcmpentity.udtdetermination.UDTDtmComponent;
import com.inspur.edp.cef.designtime.api.operation.CommonDetermination;
import com.inspur.edp.cef.designtime.api.operation.CommonOperation;
import com.inspur.edp.cef.generator.DotNetToJavaStringHelper;
import com.inspur.edp.cef.variable.dtgenerator.vardtmGenerate.JavaCompCodeNames;

public class CommonDtmGenerator extends CommonOperationComponentGenerator
{
	public static CommonDtmGenerator getInstance(){return new CommonDtmGenerator();}
	private CommonDtmGenerator()
	{
	}

	@Override
	protected GspComponent BuildComponent()
	{
		return new UDTDtmComponent();
	}

	@Override
	protected void EvaluateComponentInfo(GspComponent component, CommonOperation operation, GspComponent originalComponent)
	{
		this.originalComponent = originalComponent;
		// 基本信息（没有参数信息）
		EvaluateComponentBasicInfo((UDTDtmComponent)component, (CommonDetermination)operation);

		// 返回值信息
		((UDTDtmComponent)((component instanceof UDTDtmComponent) ? component : null)).getUdtDtmMethod().setReturnValue(new ReturnValue());
	}
	private void EvaluateComponentBasicInfo(UDTDtmComponent component, CommonDetermination determination)
	{
		if (!DotNetToJavaStringHelper.isNullOrEmpty(determination.getComponentId()))
		{
			component.setComponentID( determination.getComponentId());
		}
		component.setComponentCode( determination.getCode());
		component.setComponentName(determination.getName());
		component.setComponentDescription( determination.getDescription());
		component.getUdtDtmMethod().setAssembly (this.assemblyName);

		String suffix=String.format("%1$s%2$s%3$s",this.model.getCode(),".", JavaCompCodeNames.DeterminationName);
		String packageName=JavaModuleImportPackage(this.nameSpace);
		packageName=String.format("%1$s%2$s",packageName,suffix.toLowerCase());
		if (this.originalComponent!= null)
		{
			component.getUdtDtmMethod().setDotnetClassName(originalComponent.getMethod().getClassName());
			component.getMethod().setClassName(JavaModuleClassName(originalComponent.getMethod().getDotnetClassName(),packageName));
		}
		else
		{
			String classNameSuffix = varEntityCode+determination.getCode();
			component.getUdtDtmMethod().setDotnetClassName(ComponentClassNameGenerator.GenerateUDTDtmComponentClassName(this.nameSpace, classNameSuffix));
			component.getMethod().setClassName(JavaModuleClassName(component.getUdtDtmMethod().getDotnetClassName(),packageName));
		}
	}
	protected String JavaModuleClassName(String classNamestr, String packageNameStr)
	{
		String connections = "";
		if (!(classNamestr==null || "".equals(classNamestr)))
		{
			connections = classNamestr.substring(classNamestr.lastIndexOf("."));
			connections = String.format("%1$s%2$s",packageNameStr, connections);
		}

		return (connections);
	}
	protected String JavaModuleImportPackage(String packageName)
	{
		String[] strArray = packageName.split("[.]",-1);
		String str = "com.";
		int i;

		for (i = 0; i < strArray.length; i++)
		{
			str += strArray[i].toLowerCase() + ".";
		}
		return str;
	}
}
